package com.nit.lab.mapper.basic;

import com.nit.lab.dto.basic.SghDTO;
import com.nit.lab.entity.basic.SghEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface SghMapper {

    /**
     * @Description: 获取表格数据，查询条件自行编写对应代码，需区分模糊和相等，排序条件无需改动
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Select({"<script>SELECT " +
            "sgh_id AS `key`, sgh_id, " +
            "sgh_name, sgh_tel, sgh_info " +
            "FROM basic_sgh " +
            "WHERE TRUE " +
            "<if test='sghName != null'> AND sgh_name LIKE CONCAT('%', #{sghName}, '%') </if>" +
            "<if test='sghTel != null'> AND sgh_tel LIKE CONCAT('%', #{sghTel}, '%') </if>" +
            "ORDER BY " +
            "<if test='sortField != null'>" +
            "${sortField} ${sortOrder}, " +
            "</if>" +
            "sgh_id desc " +
            "LIMIT #{start}, #{length}" +
            "</script>"})
    List<SghDTO> list4Table(
            @Param("start") Integer start,
            @Param("length") Integer length,
            @Param("sghName") String sghName,
            @Param("sghTel") String sghTel,
            @Param("sortField") String sortField,
            @Param("sortOrder") String sortOrder) throws Exception;

    /**
     * @Description: 获取表格数据记录的总条数
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Select({"<script>SELECT COUNT(*) " +
            "FROM basic_sgh " +
            "WHERE TRUE " +
            "<if test='sghName != null'> AND sgh_name LIKE CONCAT('%', #{sghName}, '%') </if>" +
            "<if test='sghTel != null'> AND sgh_tel LIKE CONCAT('%', #{sghTel}, '%') </if>" +
            "</script>"})
    Long count4Table(
            @Param("sghName") String sghName,
            @Param("sghTel") String sghTel) throws Exception;

    /**
     * @Description: 新增记录
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Insert("INSERT INTO basic_sgh (" +
            "sgh_name, sgh_tel, sgh_info ) " +
            "VALUES (#{sghEntity.sghName}, #{sghEntity.sghTel}, " +
            "#{sghEntity.sghInfo})")
    @Options(useGeneratedKeys = true, keyProperty = "userId", keyColumn = "sgh_id")
    Integer add(@Param("SghEntity") SghEntity sghEntity) throws Exception;

    /**
     * @Description: 更新记录
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Update("UPDATE basic_sgh " +
            "sgh_name = #{userEntity.sghName}, " +
            "sgh_tel = #{userEntity.sghTel}, " +
            "sgh_info = #{SghEntity.sghInfo}, " +
            "WHERE sgh_id = #{SghEntity.sghId}")
    Integer update(@Param("sghEntity") SghEntity sghEntity) throws Exception;


    /**
     * @Description: 删除记录
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Delete("DELETE FROM basic_sgh WHERE sgh_id = #{sghId}")
    Integer remove(@Param("sghId") Long sghId) throws Exception;
}
